package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerfilestandardController")
public class CrawlerfilestandardController extends CmiEmaController {

	@RequestMapping("/listCrawlerfilestandards")
	public ModelAndView listCrawlerfilestandards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilestandardPaginationService.updatePagination(request, pagination);
		Set<Crawlerfilestandard> crawlerfilestandards = crawlerfilestandardPaginationService
				.getPaginationCrawlerfilestandards(pagination);

		mav.addObject("crawlerfilestandards", crawlerfilestandards);
		mav.addObject("entityName", "crawlerfilestandard");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfilestandard/listCrawlerfilestandards.jsp");
		return mav;
	}

	public String listCrawlerfilestandards() {
		return "redirect:/listCrawlerfilestandards";
	}

	@RequestMapping("/viewCrawlerfilestandard")
	public ModelAndView viewCrawlerfilestandard(@RequestParam Integer crawlerfilestandardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfilestandard",
				crawlerfilestandardDAO.findCrawlerfilestandardByPrimaryKey(crawlerfilestandardId));
		mav.setViewName("crawlerfilestandard/viewCrawlerfilestandard.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfilestandards")
	public ModelAndView confirmDeleteCrawlerfilestandards(
			@RequestParam(value = "deleteCrawlerfilestandardsIds", required = true) Set<Integer> deleteCrawlerfilestandardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfilestandard> deleteCrawlerfilestandards = crawlerfilestandardDAO
				.findCrawlerfilestandardsByIds(deleteCrawlerfilestandardsIds);
		Set<Crawlerfilestandard> referencedCrawlerfilestandards = crawlerfilestandardService
				.loadReferenceCrawlerfilestandards(deleteCrawlerfilestandards);
		boolean hasReferenced;
		if (referencedCrawlerfilestandards.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfilestandards);
			hasReferenced = false;
		} else {
			deleteCrawlerfilestandards.removeAll(referencedCrawlerfilestandards);
			mav.addObject("referencedEntitys", referencedCrawlerfilestandards);
			mav.addObject("deleteEntitys", deleteCrawlerfilestandards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfilestandard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfilestandards");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandard")
	public String deleteCrawlerfilestandard(@RequestParam Integer crawlerfilestandardId) {
		Crawlerfilestandard crawlerfilestandard = crawlerfilestandardDAO
				.findCrawlerfilestandardByPrimaryKey(crawlerfilestandardId);
		crawlerfilestandardService.deleteCrawlerfilestandard(crawlerfilestandard);
		return "forward:/listCrawlerfilestandards";
	}

	@RequestMapping("/confirmDeleteCrawlerfilestandard")
	public ModelAndView confirmDeleteCrawlerfilestandard(@RequestParam Integer crawlerfilestandardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfilestandard",
				crawlerfilestandardDAO.findCrawlerfilestandardByPrimaryKey(crawlerfilestandardId));

		mav.setViewName("crawlerfilestandard/deleteCrawlerfilestandard.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandards")
	public String deleteCrawlerfilestandards(
			@RequestParam(value = "deleteCrawlerfilestandardsId", required = true) Set<Integer> deleteCrawlerfilestandardsId) {
		crawlerfilestandardService.deleteCrawlerfilestandards(deleteCrawlerfilestandardsId);
		return "forward:/listCrawlerfilestandards";
	}

	@RequestMapping("/editCrawlerfilestandard")
	public ModelAndView editCrawlerfilestandard(@RequestParam Integer crawlerfilestandardId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandard crawlerfilestandard = crawlerfilestandardDAO
				.findCrawlerfilestandardByPrimaryKey(crawlerfilestandardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfilestandard", crawlerfilestandard);

		// 把所有的引用crawlerfilestandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardService.loadCrawlerfilestandardObjectReferencedSet(crawlerfilestandard));

		// 将编辑页面crawlerfilestandard添加到mav中
		mav.setViewName("crawlerfilestandard/editCrawlerfilestandard.jsp");
		return mav;
	}

	@RequestMapping("/newCrawlerfilestandard")
	public ModelAndView newCrawlerfilestandard() {
		ModelAndView mav = new ModelAndView();
		Crawlerfilestandard crawlerfilestandard = new Crawlerfilestandard();

		mav.addObject("crawlerfilestandard", crawlerfilestandard);

		// 把所有的引用crawlerfilestandard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardService.loadCrawlerfilestandardObjectReferencedSet(crawlerfilestandard));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfilestandard/editCrawlerfilestandard.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawlerfilestandard")
	public ModelAndView saveCrawlerfilestandard(@ModelAttribute Crawlerfilestandard crawlerfilestandard) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandard existingCrawlerfilestandard = CmiHibernateProxyUtil.deproxy(
				crawlerfilestandardDAO.findCrawlerfilestandardByPhysicalPrimaryKey(crawlerfilestandard.getName()));

		// 新建记录
		if (crawlerfilestandard.getId() == null) {
			if (crawlerfilestandard.equals(existingCrawlerfilestandard)) {

				mav.addAllObjects(
						crawlerfilestandardService.loadCrawlerfilestandardClassReferencedSet(crawlerfilestandard));
				// mav = editContinue(mav, crawlerfilestandard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandard/editCrawlerfilestandard.jsp");
			} else {
				crawlerfilestandardService.saveCrawlerfilestandard(crawlerfilestandard);
				mav.setViewName("forward:/listCrawlerfilestandards");
			}
		}
		// 编辑记录
		else {
			Crawlerfilestandard originCrawlerfilestandard = CmiHibernateProxyUtil
					.deproxy(crawlerfilestandardDAO.findCrawlerfilestandardByPrimaryKey(crawlerfilestandard.getId()));
			if (!crawlerfilestandard.equals(originCrawlerfilestandard)
					&& crawlerfilestandard.equals(existingCrawlerfilestandard)) {
				mav.addAllObjects(
						crawlerfilestandardService.loadCrawlerfilestandardClassReferencedSet(crawlerfilestandard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandard/editCrawlerfilestandard.jsp");
			} else {
				crawlerfilestandardService.saveCrawlerfilestandard(crawlerfilestandard);
				mav.setViewName("forward:/listCrawlerfilestandards");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfilestandardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
